package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.xm.Similarity;

import java.util.Arrays;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

@SpringBootApplication
public class DemoApplication {










                public static void main(String[] args) {
                    String[] phrases1 = {"phrase1", "phrase2", "phrase3", "phrase4"};
                    // 示例输入数组
                    String[] phrases = {
                            "孝弟也者道",
                            "千乘之国道",
                            "政齐之以道",
                            "道之以政",
                            "不行乘道",
                            "一变至道",
                            "贵乎道者",
                            "邦不入道",
                            "之乎者道",
                            "朝闻道",
                            "夫子之言道",
                            "道者三道",
                            "之道也道",
                            "者四焉道",
                            "君子食道",
                            "居无求道",
                            "无改于道",
                            "志于道",
                            "适道未道",
                            "力不足道",
                            "可谓孝道",
                            "可与共道",
                            "道不行道",
                            "大夫者道",
                            "道将行道",
                            "之道者道",
                            "之以德道",
                            "与道道",
                            "可矣道",
                            "与德道",
                            "如有所道",
                            "者三道",
                            "是所谓道",
                            "力不同道",
                            "之于人道",
                            "之于天道",
                            "之将行道",
                            "有所誉道",
                            "者不忧道",
                            "子言卫道"
                    };
                    // 示例输入数组
                    String[] phrases2 = {"phrase1", "phrase2", "phrase3", "phrase4"};
                    int n = phrases.length;

                    // 创建 n*n 的结果数组
                    double[][] resultMatrix = new double[n][n];

                    // 计算两两对比的相似度
                    for (int i = 0; i < n; i++) {
                        for (int j = 0; j < n; j++) {
                            resultMatrix[i][j] = Similarity.phraseSimilarity(phrases[i], phrases[j]);
                        }
                    }

                    // 找到最大三个值及其索引
                    Set<Double> maxValuesSet = new HashSet<>(); // 用于存储唯一的最大值
                    int[] maxIndices = new int[20]; // 存储对应的索引
                    double[] maxValues = new double[20];
                    Arrays.fill(maxValues, Double.NEGATIVE_INFINITY); // 初始化为负无穷

                    for (int i = 0; i < n; i++) {
                        for (int j = 0; j < n; j++) {
                            // 跳过对角线元素
                            if (i != j) {
                                double value = resultMatrix[i][j];
                                // 检查是否为最大值
                                if (maxValuesSet.size() < 20 || value > maxValues[0]) {
                                    maxValuesSet.add(value);
                                    // 更新最大值和索引
                                    for (int k = 0; k < maxValues.length; k++) {
                                        if (value > maxValues[k]) {
                                            // 移动较小的值
                                            for (int l = maxValues.length - 1; l > k; l--) {
                                                maxValues[l] = maxValues[l - 1];
                                                maxIndices[l] = maxIndices[l - 1];
                                            }
                                            maxValues[k] = value;
                                            maxIndices[k] = i * n + j; // 存储元素位置 (i, j)
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }

                    // 输出结果矩阵
                    System.out.println("相似度矩阵：");
                    for (int i = 0; i < n; i++) {
                        for (int j = 0; j < n; j++) {
                            System.out.print(resultMatrix[i][j] + " ");
                        }
                        System.out.println();
                    }

                    // 输出最大三个唯一值及其位置
                    System.out.println("除对角线外最大的三个唯一值及其索引：");
                    for (int k = 0; k < maxValues.length; k++) {
                        if (maxValues[k] != Double.NEGATIVE_INFINITY) { // 确保值有效
                            int index = maxIndices[k];
                            int row = index / n;
                            int col = index % n;
                            System.out.println("值: " + maxValues[k] + "，位置: (" + row + ", " + col + ")");
                        }
                    }
                }
            }




